<?php

namespace App\Services;

use App\Http\Resources\Mini\FeedbackClassConllection;
use App\Models\FeedbackClass;
use Illuminate\Http\Request;
use Mockery\Exception;

class FeedbackClassService extends BaseService
{
    public $model;

    public function __construct()
    {
        $this->model = new FeedbackClass();
    }


    /**
     * @param int $limit
     * @param bool $if_paginate
     * @return array
     */
    public function getList($limit = 30, $if_paginate = true, $if_code_return = true)
    {
        $where = [];
        if (request()->is_show) {
            $where['is_show'] = 0;
        }
        $object = $this->model->where($where);
        if (\request()->keywords) {
            $object->where('feedback_class_name', \request()->keywords);
        }
        if ($if_paginate) {
            $list = $object->orderBy('sort', 'desc')->paginate($limit)->toArray();
        } else {
            $list = $object->orderBy('sort', 'desc')->select()->get()->toArray();
        }

        if ($if_code_return) {
            return $this->success(new FeedbackClassConllection($list));
        } else {
            return $this->format($list);
        }
    }

    /**
     * @param Request $request
     */
    public function add()
    {
        try {
            $data = [
                'feedback_class_name' => request()->feedback_class_name,
                'sort' => request()->sort,
                'is_show' => request()->is_show
            ];

            if ($this->model->where('feedback_class_name', $data['feedback_class_name'])->first()) {
                return $this->error('请勿重复添加分类');
            }
            $this->model->create($data);
            return $this->success([], '执行成功');
        } catch (\Exception $e) {
            return $this->error('执行失败' . $e->getMessage());
        }

    }


    /**
     * 修改
     * @param Request $request
     * @return array
     */
    public function edit($id)
    {
        try {
            $data = [
                'feedback_class_name' => request()->feedback_class_name,
                'sort' => request()->sort,
            ];
            $this->model->where('id', $id)->update($data);
            return $this->success([], '修改成功');
        } catch (\Exception $e) {
            return $this->error([], '执行失败' . $e->getMessage());
        }
    }


    /**
     *删除 反馈分类
     */
    public function del($ids)
    {
        return $this->deletes($ids) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * @return array 数组
     */
    public function show($id)
    {
        try {
            $data = $this->model->where('id', $id)->first()->toArray();
            return $this->success($data, '执行成功');
        } catch (\Exception $e) {
            return $this->error([], '执行失败' . $e->getMessage());
        }
    }

    // 删除
    public function deletes($ids)
    {
        $ids = explode(',', $ids);
        try {
            foreach ($ids as $key => $val) {
                $this->model->where('id', $val)->delete();
            }
            return true;
        } catch (Exception $exception) {
            return false;
        }
    }

    public function __destruct()
    {
        $this->model;
        // TODO: Implement __destruct() method.
    }
}
